﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Common;
using GtsServer.Manage.Controller;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// ChanceTypeAjax 的摘要说明
    /// </summary>
    public class ChanceTypeAjax : IHttpHandler
    {
        ChanceTypeController controller = new ChanceTypeController();
        ChanceMouldController mouldController = new ChanceMouldController();
        ChanceController chanceController = new ChanceController();
        EventMouldController eventMouldController = new EventMouldController();
        EventChanceController eventChanceController = new EventChanceController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void Delete(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            chanceController.DeleteByTypeID(id);
            eventChanceController.DeleteByChanceTypeID(id);
            context.Response.Write(controller.Delete(id));
        }

        private void Add(HttpContext context)
        {
            string name = context.Request["name"];
            int handCount = Convert.ToInt32(context.Request["handCount"].ToString());
            int id = Convert.ToInt32(context.Request["id"].ToString());
            bool isSuccess = false;
            if (!string.IsNullOrEmpty(name.Trim()))
            {
                ChanceType modle = new ChanceType()
                {
                    ID = id,
                    Name = name,
                    HandCount = handCount
                };
                isSuccess = controller.Add(modle);

                if (isSuccess)
                {
                    List<ChanceMould> listMould = mouldController.GetList();
                    foreach (var item in listMould)
                    {
                        Chance chance = new Chance()
                        {
                            TypeID = id,
                            ChanceMouldID = item.ID,
                            ChanceOperator = "=",
                            MaxNum = 0,
                            MinNum = 0,

                        };
                        isSuccess = chanceController.Add(chance);
                    }
                    //加载事件概率
                    List<EventChanceMould> mouldList = eventMouldController.GetList();
                    foreach (var item in mouldList)
                    {
                        EventChance mouldModel = new EventChance()
                        {
                            ChanceTypeID = id,
                            ChanceValue = 0,
                            EventID = item.EventID,
                            EventMouldID = item.ID,
                        };
                        isSuccess = eventChanceController.Add(mouldModel);
                    }
                }
            }
            context.Response.Write(isSuccess);
        }

        private void Update(HttpContext context)
        {
            string name = context.Request["name"];
            int handCount = Convert.ToInt32(context.Request["handCount"].ToString());
            int id = Convert.ToInt32(context.Request["id"].ToString());
            ChanceType model = new ChanceType()
            {
                ID = id,
                Name = name,
                HandCount = handCount
            };
            context.Response.Write(controller.Update(model));
        }

        private void GetList(HttpContext context)
        {
            List<ChanceType> list = controller.GetList();
            string json = JsonHelper.ListToJson<ChanceType>(list);
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}